package com.linln.admin.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linln.admin.business.controller.validator.AttendanceApplyValid;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.component.shiro.ShiroUtil;
import com.linln.modules.business.domain.AttendanceApply;
import com.linln.modules.business.service.impl.AttendanceApplyServiceImpl;
import com.linln.modules.business.service.impl.TaskServiceImpl;
import com.linln.modules.system.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chaunceyzhong
 */
@Controller
@RequestMapping("/business/attendanceApply")
public class AttendanceApplyController {

    @Autowired
    private AttendanceApplyServiceImpl attendanceApplyService;
    @Autowired
    private TaskServiceImpl taskService;


    @RequestMapping("/index")
    public String index(Model model) {
        IPage<AttendanceApply> page = attendanceApplyService.getMyAttendanceApply();
        model.addAttribute("page", page);
        model.addAttribute("list", page.getRecords());
        return "/business/attendanceApply/index";
    }

    @RequestMapping("/add")
    public String add() {
        return "/business/attendanceApply/add";
    }

    /**
     * 添加加班申请
     *
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultVo addAttendanceApply(@Validated AttendanceApplyValid attendanceApplyValid, HttpServletRequest request) {
        User user = ShiroUtil.getSubject();
        String timeSplit = " - ";
        AttendanceApply attendanceApply = new AttendanceApply();
        String time = request.getParameter("time");
        String startTime = time.split(timeSplit)[0];
        String endTime = time.split(timeSplit)[1];
        String remark = request.getParameter("remark");
        attendanceApply.setStartTime(LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        attendanceApply.setEndTime(LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        attendanceApply.setUserId(user.getId());
        attendanceApply.setUserName(user.getUsername());
        attendanceApply.setRemark(remark);
        attendanceApplyService.addAttendanceApply(attendanceApply);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 获取我的加班申请
     *
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResultVo<List<AttendanceApply>> getMyAttendanceApply() {
        ResultVo resultVo = ResultVoUtil.success();
        Long userId = ShiroUtil.getSubject().getId();

        QueryWrapper<AttendanceApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Map m = new HashMap<>(8);
        m.put("user_id", userId);
        List<AttendanceApply> attendanceApplyList = attendanceApplyService.list(queryWrapper);
        resultVo.setData(attendanceApplyList);
        return resultVo;
    }

    @RequestMapping("/auditView")
    public String auditView(Model model) {
        IPage<AttendanceApply> iPage = attendanceApplyService.getMyAudition();
        model.addAttribute("page", iPage);
        model.addAttribute("list", iPage.getRecords());
        return "/business/attendanceApply/audit";
    }

    @RequestMapping("/audit/{taskId}/{pass}")
    @ResponseBody
    public ResultVo audit(@PathVariable Long taskId, @PathVariable Integer pass) {
        attendanceApplyService.audit(taskId, pass);
        return ResultVoUtil.success("审批完成");
    }
}
